package com.example2016.cjj.mycustomviewdemo.MyView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.example2016.cjj.mycustomviewdemo.R;

/**
 * Created by CJJ on 2017/2/27.
 */

public class CustomRingView  extends View{

    private static final String TAG = "CustomRingView";

    private int mFirstColor= Color.GRAY;

    private int mSecondColor= Color.BLACK;

    private int mCircleWidth = 20;

    private Bitmap mImageBg;

    private Paint mPaint;


    //每一个弧的角度
    private int sweepangle=10;

    //第一个画弧的角度
    private int arcstartangle = 145;

    //弧的个数（也就是音量的最大数）
    private  int arcTotleCount=13;

    //弧的现在个数（也就是当前音量数）
    private int arcCurrentCount = 3;

    public CustomRingView(Context context) {
        this(context,null);
    }

    public CustomRingView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomRingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomRingView,defStyleAttr,0);
        int n = a.getIndexCount();
        for(int i=0; i< n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.MyCustomRingView_firstColor:
                    mFirstColor = a.getColor(attr,mFirstColor);
                    break;
                case R.styleable.MyCustomRingView_secondColor:
                    mSecondColor = a.getColor(attr,mSecondColor);
                    break;
                case R.styleable.MyCustomRingView_circleWidth:
                    mCircleWidth = a.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,mCircleWidth,getResources().getDisplayMetrics()) );

                    break;
                case R.styleable.MyCustomRingView_imagebg:
                    mImageBg= BitmapFactory.decodeResource( getResources(), a.getResourceId(attr,0) );
                    break;

            }

        }

        a.recycle();

        mPaint = new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(mFirstColor);
        mPaint.setStrokeWidth(20);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);

        //圆心的x轴
        int center = getWidth()/2;

        //内圆的半径
        int radius = center - mCircleWidth/2;


//        canvas.drawArc(rectF,a[position],b[position],false,mPaint);
//        第一个参数是一个矩形  相当于圆弧半径包裹的矩形
//        第二个参数角度是按逆时针方向到3点钟位置的角度 ，也就是从哪一起始点开始画。
//        第三个参数是画多少角度正值为顺时针 反之为逆时针。
//        第四个参数为是否显示弧到圆心的那条半径
//         第5个为画笔
       // canvas.drawArc(rectF,135,270,false,mPaint);

        //画弧的矩形
        RectF rectF = new RectF(center - radius, center - radius, center + radius, center + radius);
        //先画画全部的音量
        for(int i =0; i< arcTotleCount;i++){
            //每隔一个画弧
            int startangle = arcstartangle + sweepangle  * i*2 ;
            mPaint.setColor(mFirstColor);
            canvas.drawArc(rectF,startangle,sweepangle,false,mPaint);
        }

        //再画当前的音量值
        for(int i =0; i< arcCurrentCount;i++){
            //每隔一个画弧
            int startangle = arcstartangle + sweepangle  * i*2 ;
            mPaint.setColor(mSecondColor);
            canvas.drawArc(rectF,startangle,sweepangle,false,mPaint);
        }


        //弧的内切矩形，用于画图片
        int a = (int) (radius * Math.sqrt(2)); //内切矩形的边长。
        Rect inRect = new Rect();
        inRect.left = (center - radius) + (radius - a/2);
        inRect.top =(center - radius) + (radius - a/2);
        inRect.right = (radius  + a/2);
        inRect.bottom = (radius  + a/2);

        canvas.drawBitmap(mImageBg,null,inRect,mPaint);

    }


    float downY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveY = event.getY();
                float dy = moveY - downY;

                if( dy > 10 ){
                    //手指向下滑动
                    voiceDown();
                }else if(dy < -10){
                    voiceUp();
                }

                downY = moveY;

                break;
            case MotionEvent.ACTION_UP:
                break;
        }


        return true;
    }




    //音量加大
    private void  voiceUp(){
        if( arcCurrentCount < arcTotleCount){
            arcCurrentCount ++;
            invalidate();
        }
    }

    //音量减少
    private void  voiceDown(){
        if( arcCurrentCount >=0){
            arcCurrentCount --;
            invalidate();
        }
    }


}

